﻿// <copyright file="ArmyImporter.cs" company="Gravity Age Studios">
// Code released under the MIT license (see License.txt).
// </copyright>
// <author>Vicente Cartas Espinel</author>
// <email>vicente.cartas@gmail.com</email>
namespace LightSwitchApplication.Import
{
    using System;
    using System.Collections.Generic;
    using System.Linq;

    internal class ArmyImporter : VisualCollectionImporter<Army>
    {
        private readonly int Visibility = 0;
        private readonly int Name = 1;
        private readonly int IsMercenary = 2;
        private readonly int Owner = 3;
        private readonly int Location = 4;
        private readonly int RecruitmentCost = 5;
        private readonly int MaintenanceCost = 6;
        private readonly int PublicInformation = 7;
        private readonly int OwnerInformation = 8;
        private readonly int GMInformation = 9;

        private IEnumerable<Domain> domains;
        private IEnumerable<DomainProvince> provinces;

        public ArmyImporter(DataWorkspace workspace)
            : base(workspace)
        {
            this.domains = this.workspace.ApplicationData.Domains.GetQuery().Execute();
            this.provinces = this.workspace.ApplicationData.DomainProvinces.GetQuery().Execute();
        }

        protected override bool SkipEntity(int index)
        {
            if (this.headers[Name].HasValue)
            {
                if (this.entities.Any(e => e.Name.Equals(data[index + headers[Name].Value].Trim(), StringComparison.InvariantCultureIgnoreCase)))
                {
                    return true;
                }
            }

            return false;
        }

        protected override int?[] MapHeaders(string[] stringHeaders)
        {
            int?[] headers = new int?[10];

            for (int i = 0; i < stringHeaders.Length; i++)
            {
                if (stringHeaders[i].StartsWith("Vis", StringComparison.InvariantCultureIgnoreCase))
                {
                    headers[Visibility] = i;
                }
                else if (stringHeaders[i].StartsWith("Name", StringComparison.InvariantCultureIgnoreCase))
                {
                    headers[Name] = i;
                }
                else if (stringHeaders[i].StartsWith("IsMerc", StringComparison.InvariantCultureIgnoreCase))
                {
                    headers[IsMercenary] = i;
                }
                else if (stringHeaders[i].StartsWith("Owner", StringComparison.InvariantCultureIgnoreCase))
                {
                    headers[Owner] = i;
                }
                else if (stringHeaders[i].StartsWith("Loc", StringComparison.InvariantCultureIgnoreCase))
                {
                    headers[Location] = i;
                }
                else if (stringHeaders[i].StartsWith("Recruit", StringComparison.InvariantCultureIgnoreCase))
                {
                    headers[RecruitmentCost] = i;
                }
                else if (stringHeaders[i].StartsWith("Maint", StringComparison.InvariantCultureIgnoreCase))
                {
                    headers[MaintenanceCost] = i;
                }
                else if (stringHeaders[i].StartsWith("Public", StringComparison.InvariantCultureIgnoreCase))
                {
                    headers[PublicInformation] = i;
                }
                else if (stringHeaders[i].StartsWith("Own", StringComparison.InvariantCultureIgnoreCase))
                {
                    headers[OwnerInformation] = i;
                }
                else if (stringHeaders[i].StartsWith("GM", StringComparison.InvariantCultureIgnoreCase))
                {
                    headers[GMInformation] = i;
                }

            }

            return headers;
        }

        protected override void FillEntity(Army entity, int index, int?[] headers, string[] data)
        {
            if (headers[Visibility].HasValue)
            {
                int value;
                if (int.TryParse(data[index + headers[Visibility].Value].Trim(), out value))
                {
                    entity.Visibility = value;
                }
            }

            if (headers[Name].HasValue)
            {
                entity.Name = data[index + headers[Name].Value].Trim();
            }

            if (headers[IsMercenary].HasValue)
            {
                bool value;
                if (bool.TryParse(data[index + headers[IsMercenary].Value].Trim(), out value))
                {
                    entity.IsMercenary = value;
                }
            }

            if (headers[RecruitmentCost].HasValue)
            {
                double value;
                if (double.TryParse(data[index + headers[RecruitmentCost].Value].Trim(), out value))
                {
                    entity.RecruitmentCost = value;
                }
            }

            if (headers[MaintenanceCost].HasValue)
            {
                double value;
                if (double.TryParse(data[index + headers[MaintenanceCost].Value].Trim(), out value))
                {
                    entity.MaintenanceCost = value;
                }
            }

            if (headers[PublicInformation].HasValue)
            {
                entity.PublicInformation = data[index + headers[PublicInformation].Value].Trim();
            }

            if (headers[OwnerInformation].HasValue)
            {
                entity.OwnerInformation = data[index + headers[PublicInformation].Value].Trim();
            }

            if (headers[GMInformation].HasValue)
            {
                entity.GMInformation = data[index + headers[GMInformation].Value].Trim();
            }
        }
    }
}
